import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ModianVisitorLogApi } from '#/api/moxi/modianvisitorlog';
import Dayjs from 'dayjs';
import { ref } from 'vue';

// 获取规则别名
import { getSysAbsenceRulePage } from '#/api/moxi/sysabsencerule';
const rulesOptions = ref<Array<{ label: string; value: string }>>([]);
const loadDeptOptions = async () => {
    try {
        const response = await getSysAbsenceRulePage({ pageNo: 1, pageSize: 100 });
        console.log('获取规则');
        console.log(response);
        rulesOptions.value = response.list.map(dept => dept.ruleAlias ? ({
            label: dept.ruleAlias,
            value: dept.ruleAlias
        }) : []);
    } catch (error) {
        console.error('获取规则失败:', error);
    }
};

loadDeptOptions();

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
    return [
        {
            fieldName: 'ruleAlias',
            label: '搜索规则',
            component: 'Select',
            componentProps: {
                options: rulesOptions,
                placeholder: '请选择规则',
                allowClear: true,
            },
        },
    ];
}

/** 列表的字段 */
export function useGridColumns<T = ModianVisitorLogApi.ModianVisitorLog>(
    onStatusChange?: (
        newStatus: number,
        row: T,
    ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
    return [
        {
            field: 'id',
            title: '序号',
        },
        {
            field: 'ruleAlias',
            title: '规则名称',
        },
        {
            field: 'createTime',
            title: '创建时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm') : '';
            },
        },
        {
            field: 'absenceThreshold',
            title: '超时时间',
             formatter: ({ cellValue }) => {
                if (!cellValue || cellValue === 0) {
                    return '0小时';
                }
                
                const hours = Math.floor(cellValue / 60);
                const minutes = cellValue % 60;
                
                if (minutes === 0) {
                    return `${hours}小时`;
                } else {
                    return `${hours}小时${minutes}分钟`;
                }
            }
        },
        {
            title: '操作',
            width: 250,
            fixed: 'right',
            slots: { default: 'actions' },
        },
    ];
}
